package org.jeecg.modules.quality.service.impl;

import org.jeecg.modules.quality.entity.QualityToReportUser;
import org.jeecg.modules.quality.entity.QualityToReportUserDetail;
import org.jeecg.modules.quality.mapper.QualityToReportUserDetailMapper;
import org.jeecg.modules.quality.mapper.QualityToReportUserMapper;
import org.jeecg.modules.quality.service.IQualityToReportUserService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 质检员主表
 * @Author: jeecg-boot
 * @Date:   2020-10-26
 * @Version: V1.0
 */
@Service
public class QualityToReportUserServiceImpl extends ServiceImpl<QualityToReportUserMapper, QualityToReportUser> implements IQualityToReportUserService {

	@Autowired
	private QualityToReportUserMapper qualityToReportUserMapper;
	@Autowired
	private QualityToReportUserDetailMapper qualityToReportUserDetailMapper;

	@Override
	@Transactional
	public void saveMain(QualityToReportUser qualityToReportUser, List<QualityToReportUserDetail> qualityToReportUserDetailList) {
		qualityToReportUserMapper.insert(qualityToReportUser);
		if(qualityToReportUserDetailList!=null && qualityToReportUserDetailList.size()>0) {
			for(QualityToReportUserDetail entity:qualityToReportUserDetailList) {
				//外键设置
				entity.setQualityToReportUserId(qualityToReportUser.getId());
				qualityToReportUserDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(QualityToReportUser qualityToReportUser,List<QualityToReportUserDetail> qualityToReportUserDetailList) {
		qualityToReportUserMapper.updateById(qualityToReportUser);

		//1.先删除子表数据
		qualityToReportUserDetailMapper.deleteByMainId(qualityToReportUser.getId());

		//2.子表数据重新插入
		if(qualityToReportUserDetailList!=null && qualityToReportUserDetailList.size()>0) {
			for(QualityToReportUserDetail entity:qualityToReportUserDetailList) {
				//外键设置
				entity.setQualityToReportUserId(qualityToReportUser.getId());
				qualityToReportUserDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		qualityToReportUserDetailMapper.deleteByMainId(id);
		qualityToReportUserMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			qualityToReportUserDetailMapper.deleteByMainId(id.toString());
			qualityToReportUserMapper.deleteById(id);
		}
	}

}
